/**
 * $Id: PersonnelResource.java 3 2011-09-05 21:38:37Z adriftrock@gmail.com $
 */

package com.swehr.server.resources;

import com.sun.jersey.api.json.JSONWithPadding;
import com.swehr.server.models.Personnel;
import com.swehr.server.models.ResultInfo;
import com.swehr.server.services.IPersonnelService;
import com.google.inject.Inject;
import org.apache.log4j.Logger;

import javax.inject.Singleton;
import javax.ws.rs.*;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * @author Rock Huang
 * @version 0.1
 */
@Singleton
@Path("/personnel")
public class PersonnelResource {
    private final static Logger logger = Logger.getLogger(PersonnelResource.class);

    @Inject private IPersonnelService service;

    @GET
    @Produces({"application/x-javascript"})
    @Path("jsonp")
    public JSONWithPadding getJsonp(@QueryParam("callback") String callback) {
        List<Personnel> orgs = service.listPsn();
        return new JSONWithPadding(new GenericEntity<List<Personnel>>(orgs){}, callback);
    }

    @GET
    @Produces({"application/x-javascript"})
    @Path("byorg")
    public JSONWithPadding byOrg(@QueryParam("callback") String callback, @QueryParam("id") String id) {
        List<Personnel> orgs = service.listPsnByOrg(id);
        return new JSONWithPadding(new GenericEntity<List<Personnel>>(orgs){}, callback);
    }

    @GET
    @Produces({"application/x-javascript"})
    @Path("bypos")
    public JSONWithPadding byPos(@QueryParam("callback") String callback, @QueryParam("id") String id) {
        List<Personnel> orgs = service.listPsnByPos(id);
        return new JSONWithPadding(new GenericEntity<List<Personnel>>(orgs){}, callback);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/save")
    public ResultInfo save(List<Personnel> emps) {
        logger.debug("Save service accept param by POST method: entity");
        logger.debug(emps.size());
        return service.savePsns(emps);
    }

}
